//
//  RegisterViewContoller.m
//  DeepBreathing
//
//  Created by rimi on 15/12/16.
//  Copyright © 2015年 rimi. All rights reserved.
//

#import "RegisterViewContoller.h"

#import "NetworkManger+User.h"




#define TEXTNUMBER_TAG 100



@interface RegisterViewContoller ()<UITextFieldDelegate>




@end

@implementation RegisterViewContoller
-(void)viewWillAppear:(BOOL)animated{
    
    [super viewWillAppear:animated];
    
    [self.navigationController setNavigationBarHidden:NO];
}

-(void)viewDidLoad{
    [super viewDidLoad];
    
    self.view.backgroundColor = [UIColor colorWithRed:0.902 green:0.902 blue:0.902 alpha:1.0];
    
    self.title = @"注册";
    
    
    
    UIImage *image = [UIImage imageNamed:@"返回"];
    
    UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
    
    button.frame = CGRectMake(0, 0, 40, 40);
    
    [button setImage:image forState:UIControlStateNormal];
    
    [button addTarget:self action:@selector(dimiss:) forControlEvents:UIControlEventTouchUpInside];
    
    UIBarButtonItem *item = [[UIBarButtonItem alloc]initWithCustomView:button];
    
    self.navigationItem.leftBarButtonItem = item;
    
    
    UILabel *label = [[UILabel alloc]initWithFrame:CGRectMake(30, CGRectGetMaxY(self.view.frame)/12 + 20, 70, 50)];
    
    label.numberOfLines = 2;
    
    label.text = @"注册      手机号";
    
    label.font = [UIFont systemFontOfSize:16];
    
    [self.view addSubview:label];
    
    UITextField *registerPhoneNumber = [[UITextField alloc]init];
    
    registerPhoneNumber.frame = CGRectMake(CGRectGetMaxX(label.frame) + 10, CGRectGetMaxY(self.view.frame)/12 + 30, 240, 40);
    
    registerPhoneNumber.placeholder = @"请输入您的手机号";
    
    registerPhoneNumber.tag = 103;
    
    registerPhoneNumber.backgroundColor = [UIColor whiteColor];
    
    registerPhoneNumber.leftView = [[UIView alloc]initWithFrame:CGRectMake(0, 0, 10, 40)];
    registerPhoneNumber.leftViewMode = UITextFieldViewModeAlways;
    
    [registerPhoneNumber.layer setBorderWidth:2];
    [registerPhoneNumber.layer setBorderColor:[UIColor colorWithRed:1.0 green:0.8 blue:0.4 alpha:0.84].CGColor];
    
    [registerPhoneNumber addTarget:self action:@selector(textFieldDidChange:) forControlEvents:UIControlEventEditingChanged];

    
    [self.view addSubview:registerPhoneNumber];
    
    
    
    NSMutableDictionary *dic = [NSMutableDictionary dictionaryWithDictionary:@{@"houlder":@[@"请输入验证码",@"请输入密码",@"请再次输入密码"],@"title":@[@"验证码",@"密码",@"确认密码"]}];
    
    for (NSInteger index = 0; index < 3; index ++) {
        UITextField *registerPhoneNumber = [[UITextField alloc]init];
        
        registerPhoneNumber.frame = CGRectMake(CGRectGetMaxX(label.frame) + 10, CGRectGetMaxY(self.view.frame)/12 + 140 + index * 50 , 240, 40);
        
        registerPhoneNumber.placeholder = dic[@"houlder"][index];
        
        registerPhoneNumber.tag = TEXTNUMBER_TAG + index;
        
        registerPhoneNumber.backgroundColor = [UIColor whiteColor];
        
        registerPhoneNumber.leftView = [[UIView alloc]initWithFrame:CGRectMake(0, 0, 10, 40)];
        registerPhoneNumber.leftViewMode = UITextFieldViewModeAlways;
        
        [registerPhoneNumber.layer setBorderWidth:2];
        [registerPhoneNumber.layer setBorderColor:[UIColor colorWithRed:1.0 green:0.8 blue:0.4 alpha:0.84].CGColor];
        
        [self.view addSubview:registerPhoneNumber];
        
        [registerPhoneNumber addTarget:self action:@selector(textFieldDidChange:) forControlEvents:UIControlEventEditingChanged];

        
    }
    
    for (NSInteger index = 0; index < 3; index ++) {
        
        UILabel *label  = [[UILabel alloc]initWithFrame:CGRectMake(30, CGRectGetMaxY(self.view.frame)/12 + 140 + index * 50, 70, 50)];
        
        label.numberOfLines = 2;
        
        label.text = dic[@"title"][index];
        
        label.font = [UIFont systemFontOfSize:16];
        
        [self.view addSubview:label];

    }
    
    UIButton *surebutton = [UIButton buttonWithType:UIButtonTypeSystem];
    
    surebutton.frame = CGRectMake(CGRectGetMaxX(label.frame) + 10, CGRectGetMaxY(self.view.frame)/12 + 80, 100, 40);
    [surebutton setTitle:@"获取验证码" forState:UIControlStateNormal];
    
    surebutton.backgroundColor = [UIColor colorWithRed:1.0 green:0.502 blue:0.0 alpha:0.92];
    
    surebutton.tintColor = [UIColor whiteColor];
    
    [surebutton.layer setBorderWidth:1];
    [surebutton.layer setBorderColor:[UIColor blackColor].CGColor];
    [surebutton addTarget:self action:@selector(getSecuritycode:) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:surebutton];
    
   
    
    UIButton *finshbutton = [UIButton buttonWithType:UIButtonTypeSystem];
    
    finshbutton.frame = CGRectMake(30, CGRectGetMaxY(self.view.frame) - 140, 315, 40);
    [finshbutton setTitle:@"完成注册" forState:UIControlStateNormal];
    finshbutton.backgroundColor = [UIColor colorWithRed:1.0 green:0.502 blue:0.0 alpha:0.92];
    
    finshbutton.tintColor = [UIColor whiteColor];
    
    [finshbutton.layer setBorderWidth:1];
    
    [finshbutton.layer setBorderColor:[UIColor blackColor].CGColor];
    
    [finshbutton addTarget:self action:@selector(finshed:) forControlEvents:UIControlEventTouchUpInside];
    
    [self.view addSubview:finshbutton];

    
    


}

-(void)dimiss:(UIButton *)sender{
    
    [self.navigationController setNavigationBarHidden:YES];
    [self.navigationController popViewControllerAnimated:YES];
    
}

-(void)getSecuritycode:(UIButton *)sender{
    UITextField *textOfphone = (UITextField *)[self.view viewWithTag:103];
    
    if (textOfphone.text.length == 11) {
        
        [NetworkManger verifPhoneNumberWithPhoneNumber:textOfphone.text successHandler:^(id responseObject) {
            NSLog(@"啊啊大 = %@",responseObject[@"status"]);
            if ([responseObject[@"status"] isEqualToString:@"0"]) {
                [NetworkManger getDetectionNumberWhenRegisingWithPhoneNumber:textOfphone.text successHander:^(id responseObject) {
                    [self otherpopuppromptBox:@"获取成功"];
                    
                } failureHander:^(NSError *error) {
                    
                }];
                
            }else if ([responseObject[@"status"] isEqualToString:@"-1"]){
                
                [self otherpopuppromptBox:@"该号码已经注册"];
                
            }else if ([responseObject[@"status"] isEqualToString:@"1"]){
                
                [self otherpopuppromptBox:@"账号被禁用"];
            }
            
        } failureHandler:^(NSError *error) {
            [self otherpopuppromptBox:@"获取失败"];
            
        }];

        
    }else{
        [self otherpopuppromptBox:@"手机号码不正确"];
    }
    
    
    
    
      
}

#pragma mark -- worning
-(void)otherpopuppromptBox:(NSString *)string{
    
    UIAlertController  *alert = [UIAlertController alertControllerWithTitle:@"温馨提示" message:string preferredStyle:UIAlertControllerStyleAlert];
    
    UIAlertAction *action = [UIAlertAction actionWithTitle:@"确定" style:UIAlertActionStyleCancel handler:^(UIAlertAction * _Nonnull action) {
        
    }];
    
    [alert addAction:action];
    [self presentViewController:alert animated:YES completion:nil];
    
    
}

-(void)finshed:(UIButton *)sender{
    
    
    
    
    UITextField *textOfphone = (UITextField *)[self.view viewWithTag:103];
    UITextField *textOfsecnumber = (UITextField *)[self.view viewWithTag:100];
    UITextField *textOfcode = (UITextField *)[self.view viewWithTag:101];
    UITextField *textofverifyCode = (UITextField *)[self.view viewWithTag:102];
    
    if (textOfphone.text.length != 11) {
        [self otherpopuppromptBox:@"请输入正确的手机号"];
        
    }else if (textOfphone.text.length == 11 && textOfsecnumber.text.length != 6){
        
        [self otherpopuppromptBox:@"请输入6位验证码"];
        
    }else if (textOfphone.text.length == 11 &&  textOfsecnumber.text.length == 6 && textOfcode.text.length < 6){
        
        [self otherpopuppromptBox:@"请输入6 - 16位密码"];
        
        
    }else if(textOfphone.text.length == 11 && textOfsecnumber.text.length == 6 && ![textOfcode.text  isEqualToString:textofverifyCode.text]){
        
        [self otherpopuppromptBox:@"密码和验证码不一致"];
    }else if(textOfphone.text.length == 11 && textOfsecnumber.text.length == 6 && [textOfcode.text  isEqualToString:textofverifyCode.text]){
        
        [NetworkManger signUpWithUserName:textOfphone.text password:textOfsecnumber.text rePwd:textOfcode.text code:textofverifyCode.text successHandler:^(id responseObject) {
            [self otherpopuppromptBox:@"注册成功"];
            [self.presentingViewController dismissViewControllerAnimated:YES completion:nil];
        } failureHandler:^(NSError *error) {
            [self otherpopuppromptBox:@"验证码错误"];
        }];

        
    }
    
    
   
    

    
}

-(void)textFieldDidChange:(UITextField *)sender{
    
    UITextField *textOfsecnumber = (UITextField *)[self.view viewWithTag:100];
    UITextField *textOfphone = (UITextField *)[self.view viewWithTag:103];
   
    UITextField *textOfcode = (UITextField *)[self.view viewWithTag:101];
    UITextField *textofverifyCode = (UITextField *)[self.view viewWithTag:102];
   
    
    if (sender  == textOfsecnumber && sender.text.length > 6) {
        
            sender.text = [sender.text substringToIndex:6];
        }
    else if (sender == textOfphone && sender.text.length > 11){
        
            sender.text = [sender.text substringToIndex:11];

        }else if (sender == textOfcode && sender.text.length > 16){
            
                sender.text = [sender.text substringToIndex:16];

            }else if (sender == textofverifyCode && sender.text.length > 16){
              
                    sender.text = [sender.text substringToIndex:16];

            }
    
    
}

@end
